package com.example.tool;

import java.util.ArrayList;
import java.util.List;

import com.example.domain.Passwd;
import com.example.domain.User;

public class PasswordTool {

    private static volatile Passwd pass;

    public static Passwd getPasswdInstance() {
        if (pass == null) {
            synchronized (Passwd.class) {
                if (pass == null) {
                    pass = new Passwd();
                }
            }
        }
        return pass;
    }

    /**
     * @param Id
     * @return 添加成功则返回添加成功信息，反之添加失败
     * @describe 单个添加用户时添加用户的初始密码
     */
    public static Passwd setPasswdInfo(Integer Id) {

        Passwd passInfo = getPasswdInstance();
        passInfo.setUserId(Id);
        passInfo.setPassword(SpringMd5Utils.getMd5("123456"));
        passInfo.setRecordTime(DateTool.getTimestamp());
        passInfo.setComment("初始密码");
        return passInfo;

    }

    /**
     * @param list1
     * @return 添加成功则返回添加成功信息，反之添加失败
     * @describe 批量添加用户时添加用户的初始密码
     */
    public static List<Passwd> setPasswdInfoByList(List<User> list1) {

        List<Passwd> list = new ArrayList<Passwd>();

        for (int i = 0; i < list1.size(); i++) {
            Passwd pass1 = new Passwd();

            pass1.setUserId(list1.get(i).getUserId());
            pass1.setPassword(SpringMd5Utils.getMd5("123456"));
            pass1.setRecordTime(DateTool.getTimestamp());
            pass1.setComment("初始密码");

            list.add(pass1);
        }

        return list;

    }

    public static List<Passwd> resetPasswordByIds(String[] pids, String[] uids, String[] time) {
        List<Passwd> list = new ArrayList<Passwd>();

        for (int i = 0; i < pids.length; i++) {
            Passwd pass1 = new Passwd();
            pass1.setPasswdId(Integer.parseInt(pids[i]));
            pass1.setUserId(Integer.parseInt(uids[i]));
            pass1.setPassword(SpringMd5Utils.getMd5("123456"));
            if (time[i] != null) {
                pass1.setRecordTime(DateTool.transferTime(time[i]));
            }
            pass1.setUpdateTime(DateTool.getTimestamp());
            pass1.setComment("初始密码");

            list.add(pass1);
        }

        return list;
    }

}
